"""
Case Type   : 系统内部使用工具
Case Name   : 数据库集群关闭后gs_ctl start -M在备节点指定pending启动
Create At   : 2021/01/18
Owner       : opentestcase015
Description :
    1.关闭正在运行的集群
    2.指定-M设置值为pending启动备机
    3.查看集群状态备机pending模式是否启动正常
    4.恢复备节点
    5.查看备机状态
    6.环境恢复
Expect      :
    1.关闭正在运行的集群成功
    2.指定-M设置值为pending启动备机成功
    3.查看集群状态备机pending模式启动正常
    4.成功
    5.备机进程状态running
    6.成功
History     :
    Modified by lopentestcase027 2023/1/9: 优化用例步骤格式及一主两备判断方式
"""

import unittest
import os
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class LogicalReplication(unittest.TestCase):
    def setUp(self):
        LOG.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.PrimaryNode = Node('PrimaryDbUser')
        self.user_node = Node('Standby1DbUser')
        self.sh_standby = CommonSH('Standby1DbUser')

    def test_system_internal_tools(self):
        LOG.info('--------step1:关闭正在运行的集群  expect:成功--------')
        is_stop = self.sh_standby.stop_db_cluster()
        self.assertTrue(is_stop)

        LOG.info('-------step2:以pending模式开启备机 expect:启动备机成功--------')
        excute_cmd = f'''
                    source {macro.DB_ENV_PATH};
                    gs_ctl start -D {macro.DB_INSTANCE_PATH} -M pending; 
                        '''
        LOG.info(excute_cmd)
        msg = self.user_node.sh(excute_cmd).result()
        LOG.info(msg)
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, msg)

        LOG.info('------step3:查看备机状态   expect:机pending模式启动正常------')
        excute_cmd = f'''
                    source {macro.DB_ENV_PATH};
                    gs_ctl query -D {macro.DB_INSTANCE_PATH}; 
                        '''
        LOG.info(excute_cmd)
        msg = self.user_node.sh(excute_cmd).result()
        LOG.info(msg)
        self.assertIn('Pending', msg)

        LOG.info('------step4:恢复备节点  expect: 成功------')
        excute_cmd = f'''
                    source {macro.DB_ENV_PATH};
                    gs_ctl restart -D {macro.DB_INSTANCE_PATH} -M standby; 
                        '''
        LOG.info(excute_cmd)
        msg = self.user_node.sh(excute_cmd).result()
        LOG.info(msg)
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, msg)

        LOG.info('------step5:查看备机状态  expect: 备机进程状态running--------')
        status = self.sh_standby.get_db_instance_status()
        self.assertTrue(status)

    def tearDown(self):
        LOG.info('------step6:环境恢复  expect:成功--------')
        excute_cmd = f'''
                        source {macro.DB_ENV_PATH};
                        gs_ctl restart -D {macro.DB_INSTANCE_PATH} -M standby; 
                            '''
        LOG.info(excute_cmd)
        msg = self.user_node.sh(excute_cmd).result()
        LOG.info(msg)
        is_start = self.sh_standby.restart_db_cluster()
        LOG.info(is_start)
        status = self.sh_standby.get_db_cluster_status(param='detail')
        LOG.info(status)
        self.assertTrue(len(status.splitlines()) >= 10)
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info(f'--------{os.path.basename(__file__)} end--------')
